#include "stm32f10x.h"                  // Device header
#include "servo.h"
#include "Delay.h"
#include "voice.h"
//舵机充当腿，括号里的值为0时表示的是腿向前进的方向甩，90度是站立

#define ACTION_DELAY_TIME 6
#define SIGNAL_DELAY_TIME 150
#define RUNING_DELAY_TIME 200

void action_attention(void)//立正, 0xA1
{
    servo_left_up(90);
    servo_light_up(90);
	Delay_ms(SIGNAL_DELAY_TIME);
    servo_left_dowm(90);
    servo_light_dowm(90);
    WeiBa = 0;
    servo_w(90);
}

//叫, 0xA2  没有相应的舵机动作

void action_sleep(void)//睡觉, 0xA3
{
    servo_left_up(0);
    servo_light_up(0);
	Delay_ms(SIGNAL_DELAY_TIME);
    servo_left_dowm(0);
    servo_light_dowm(0);
    servo_w(180);
}

void action_wag_tail(void)//摇尾巴, 0xA6
{
	Delay_ms(150);
    while (Action_Mode == enum_wag_tail) {
        for (uint8_t i = 30; i < 150; i++) {
            servo_w(i);
            Delay_ms(ACTION_DELAY_TIME);
        }
        for (uint8_t i = 150;i > 30; i--) {
            servo_w(i);
            Delay_ms(ACTION_DELAY_TIME);
        }
        Delay_ms(SIGNAL_DELAY_TIME);
    }
    Delay_ms(150);
}

void action_fall(void)//趴下, 0xA4
{
    servo_left_up(40);
    servo_light_up(30);
	Delay_ms(SIGNAL_DELAY_TIME);
    servo_left_dowm(30);
    servo_light_dowm(40);
    if (WeiBa == 1) {
		Action_Mode = enum_wag_tail;
	} else {
        servo_w(0);
    }
}

void action_squat(void)//蹲下, 0xA5 | 坐下, 0xA9
{
    servo_left_up(90);
    servo_light_up(90);
	Delay_ms(SIGNAL_DELAY_TIME);
    servo_left_dowm(30);
    servo_light_dowm(40);
    if (WeiBa == 1) {
		Action_Mode = enum_wag_tail;
	} else {
        servo_w(0);
    }
}

void action_swing(void)//摇摆, 0xA7
{
    action_attention();
	Delay_ms(SIGNAL_DELAY_TIME);

    while (Action_Mode == enum_swing) {
		for (uint8_t i = 40; i < 140; i++) {
			servo_left_up(i);
			servo_light_up(i);
			servo_left_dowm(i);
			servo_light_dowm(i);
			Delay_ms(ACTION_DELAY_TIME);
            if (Action_Mode != enum_swing)
                break;
		}
		for (uint8_t i = 140; i > 40; i--) {
			servo_left_up(i);
			servo_light_up(i);
			servo_left_dowm(i);
			servo_light_dowm(i);
			Delay_ms(ACTION_DELAY_TIME);
            if (Action_Mode != enum_swing)
                break;
		}
		if (Action_Mode != enum_swing)
            break;
	}
}

void action_handshake(void)//握手, 0xA8
{
    action_attention();
	action_squat();
	while (Action_Mode == enum_handshake) {
		if (Action_Mode != enum_handshake) break;
       for (uint8_t i = 46; i >= 1; i--) {
           if(Action_Mode != enum_handshake) break;
           servo_light_up(i);
           Delay_ms(ACTION_DELAY_TIME);
       }
       for (uint8_t i = 1;i <= 46; i++) {
           if (Action_Mode != enum_handshake) break;
           servo_light_up(i);
           Delay_ms(ACTION_DELAY_TIME);
       }

        Delay_ms(SIGNAL_DELAY_TIME);
    }
    servo_light_up(90);
}

void action_forward(void)//前进, 0xB1
{
    action_attention();
	Delay_ms(SIGNAL_DELAY_TIME);

    while (Action_Mode == enum_forward) {
        servo_light_up(45);	
        servo_left_dowm(45);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;
        servo_left_up(135);	
        servo_light_dowm(135);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;
        servo_light_up(90);	
        servo_left_dowm(90);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;
        servo_left_up(90);	
        servo_light_dowm(90);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;

        servo_left_up(45);	
        servo_light_dowm(45);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;
        servo_light_up(135);	
        servo_left_dowm(135);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;
        servo_left_up(90);	
        servo_light_dowm(90);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;
        servo_light_up(90);	
        servo_left_dowm(90);
        Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_forward) break;
	}
}

void action_retreat(void)//后退, 0xB2
{
    action_attention();
	Delay_ms(SIGNAL_DELAY_TIME);

	while (Action_Mode == enum_retreat) {
		servo_light_up(135);	
		servo_left_dowm(135);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;
		servo_left_up(45);
		servo_light_dowm(45);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;
		servo_light_up(90);	
		servo_left_dowm(90);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;
		servo_left_up(90);	
		servo_light_dowm(90);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;

		servo_left_up(135);	
		servo_light_dowm(135);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;
		servo_light_up(45);	
		servo_left_dowm(45);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;
        servo_left_up(90);	
		servo_light_dowm(90);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;
        servo_light_up(90);	
		servo_left_dowm(90);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_retreat) break;
	}
}

void action_turn_left(void)//左转, 0xB3
{
    action_attention();
	Delay_ms(SIGNAL_DELAY_TIME);

	while (Action_Mode == enum_turn_left) {
		servo_light_up(45);
		servo_left_dowm(135);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_left) break;
		servo_left_up(45);
		servo_light_dowm(135);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_left) break;
		servo_light_up(90);
		servo_left_dowm(90);	
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_left) break;
		servo_left_up(90);
		servo_light_dowm(90);	
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_left) break;
	}
}


void action_turn_right(void)//右转, 0xB4
{
    action_attention();
	Delay_ms(SIGNAL_DELAY_TIME);

	while (Action_Mode == enum_turn_right) {
		servo_left_up(45);
		servo_light_dowm(135);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_right) break;
		servo_light_up(45);
		servo_left_dowm(135);
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_right) break;
		servo_left_up(90);
		servo_light_dowm(90);	
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_right) break;
		servo_light_up(90);
		servo_left_dowm(90);	
		Delay_ms(RUNING_DELAY_TIME);
        if (Action_Mode != enum_turn_right) break;
	}
}

//向前跳, 0xB5  TODO
//向后跳, 0xB6  TODO
